﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeiXinSDK.MP.Model
{
	/// <summary>
	/// 分组群发消息请求基类
	/// </summary>
	public abstract class MassMessage2GroupModel
	{
		protected MassMessage2GroupModel(int? groupId)
		{
			Filter = new FilterSetting
			{
				IsToAll = !groupId.HasValue,
				GroupId = groupId.HasValue ? groupId.Value.ToString() : ""
			};
		}

		[JsonProperty("filter")]
		public FilterSetting Filter { get; private set; }

		[JsonProperty("msgtype")]
		public abstract string Msgtype { get; }

		/// <summary>
		/// 群发过滤
		/// </summary>
		public class FilterSetting
		{
			/// <summary>
			/// 是否发送给所有人
			/// </summary>
			[JsonProperty("is_to_all")]
			public bool IsToAll { get; set; }


			[JsonProperty("group_id")]
			public string GroupId { get; set; }
		}
	}

	public class MassNews2GroupModel : MassMessage2GroupModel
	{
		/// <summary>
		/// 创建一个群发多图文消息
		/// </summary>
		/// <param name="groupId">目标分组</param>
		/// <param name="mediaId">上传图文消息素材接口返回值</param>
		public MassNews2GroupModel(int groupId, string mediaId)
			: base(groupId)
		{
			News = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("mpnews", Order = 1)]
		public MediaItem News { get; private set; }

		public override string Msgtype { get { return "mpnews"; } }
	}

	/// <summary>
	/// 群发文本消息
	/// </summary>
	public class MassText2GroupModel : MassMessage2GroupModel
	{
		public MassText2GroupModel(int groupId, string content)
			: base(groupId)
		{
			Content = new ContentItem { Content = content };
		}

		[JsonProperty("text")]
		public ContentItem Content { get; private set; }

		public override string Msgtype { get { return "text"; } }
	}

	public class MassVoice2GroupModel : MassMessage2GroupModel
	{
		public MassVoice2GroupModel(int groupId, string mediaId)
			: base(groupId)
		{
			Voice = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("voice")]
		public MediaItem Voice { get; private set; }


		public override string Msgtype { get { return "voice"; } }
	}

	public class MassImage2GroupModel : MassMessage2GroupModel
	{
		public MassImage2GroupModel(int groupId, string mediaId)
			: base(groupId)
		{
			Image = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("image")]
		public MediaItem Image { get; private set; }


		public override string Msgtype { get { return "image"; } }
	}

	public class MassVideo2GroupModel : MassMessage2GroupModel
	{
		public MassVideo2GroupModel(int groupId, string mediaId)
			: base(groupId)
		{
			Video = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("mpvideo")]
		public MediaItem Video { get; private set; }


		public override string Msgtype { get { return "mpvideo"; } }
	}


	/// <summary>
	/// 按用户列表群发消息请求基类
	/// </summary>
	public abstract class MassMessage2UserListModel
	{
		protected MassMessage2UserListModel(IEnumerable<string> userList)
		{
			UserList = userList;
		}

		[JsonProperty("touser")]
		public IEnumerable<string> UserList { get; private set; }

		[JsonProperty("msgtype")]
		public abstract string MsgType { get; }
	}


	public class MassNews2UserListModel : MassMessage2UserListModel
	{
		public MassNews2UserListModel(IEnumerable<string> userList, string mediaId)
			: base(userList)
		{
			News = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("mpnews")]
		public MediaItem News { get; set; }

		public override string MsgType { get { return "mpnews"; } }
	}

	public class MassText2UserListModel : MassMessage2UserListModel
	{
		public MassText2UserListModel(IEnumerable<string> userList, string content)
			: base(userList)
		{
			Text = new ContentItem { Content = content };
		}

		[JsonProperty("text")]
		public ContentItem Text { get; set; }

		public override string MsgType { get { return "text"; } }
	}


	public class MassVoice2UserListModel : MassMessage2UserListModel
	{
		public MassVoice2UserListModel(IEnumerable<string> userList, string mediaId)
			: base(userList)
		{
			Voice = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("voice")]
		public MediaItem Voice { get; set; }

		public override string MsgType { get { return "voice"; } }
	}
	public class MassImage2UserListModel : MassMessage2UserListModel
	{
		public MassImage2UserListModel(IEnumerable<string> userList, string mediaId)
			: base(userList)
		{
			Image = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("image")]
		public MediaItem Image { get; set; }

		public override string MsgType { get { return "image"; } }
	}

	public class MassVideo2UserListModel : MassMessage2UserListModel
	{
		public MassVideo2UserListModel(IEnumerable<string> userList, string mediaId)
			: base(userList)
		{
			Video = new MediaItem { MediaId = mediaId };
		}

		[JsonProperty("video")]
		public MediaItem Video { get; set; }

		public override string MsgType
		{
			get { return "video"; }
		}
	}

	public class MassResponseModel : ResponseModel
	{
		[JsonProperty("msg_id")]
		public int MsgId { get; set; }
	}

	public class MassStatus : ResponseModel
	{
		public override bool IsCompleted { get { return MsgStatus == "SEND_SUCCESS"; } }

		[JsonProperty("msg_id")]
		public int MsgId { get; set; }

		/// <summary>
		/// 消息发送后的状态，SEND_SUCCESS表示发送成功
		/// </summary>
		[JsonProperty("msg_status")]
		public string MsgStatus { get; set; }
	}

	public class MediaItem
	{
		[JsonProperty("media_id")]
		public string MediaId { get; set; }
	}

	public class ContentItem
	{
		[JsonProperty("content")]
		public string Content { get; set; }
	}

	public class MsgItem
	{
		[JsonProperty("msg_id")]
		public int MsgId { get; set; }
	}

}
